//
//  Player.m
//  fish
//
//  Created by 袁飞 on 12-8-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Player.h"
#import "FishManager.h"

@implementation Player
@synthesize depth = depth_;
@synthesize net_attack = net_attack_;
@synthesize net_range = net_range_;
@synthesize net_distance = net_distance_;
@synthesize dive_dur = dive_dur_;
@synthesize firing = firing_;
@synthesize property = property_;


static Player *_sharedPlayer = nil;
+ (Player *)sharedPlayer
{
  if (!_sharedPlayer) {
        _sharedPlayer = [[Player alloc] init];
  }
  return _sharedPlayer;
}

-(id)init{
  depth_ = 1000;
  net_distance_ = 100;
  NSMutableDictionary * tmp = [NSMutableDictionary dictionary];
  [tmp setObject:@"trophies_babyhorror" forKey:@"fishno"];
  [super initWithXml:@"player.xml" withDict:tmp];
  firing_ = false;
  FAI *ai = [[PlayerAI alloc] initWithFObject:self];
  [self setAi:ai];
  [self startAi];
  return self;
}

-(void)load{
  NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  [defaults objectForKey:@"depth"];
  //NSString *name =
  //[defaults synchronize];
}

-(void)fireWithAngle:(CGPoint)point{
  CGPoint p = [self position];
  float a = point.y - p.y;
  float b = point.x - p.x;
  float c = ccpDistance(p, point); 
  float sin = a/c; 
  float cos = b/c;
  CGPoint dp = ccp(cos*net_distance_, sin*net_distance_);
  //[self moveBy:dp Dur:0.2 withSprite:@"bullet"];
  //CCSprite *sp = [self getSpriteByName:@"bullet"];
  //[sp setScale:0.5];
  
  float angle = 180/3.14*atanf(a/b);
  if (point.x < p.x) {
    if (a/b < 0) {
      angle = 180+angle;
    }else{
      angle = 180 + angle;
    }
  }
  //[sp setRotation:90-angle];
 
  FObject * bullet = [FObject FObjectFromXmlFile:@"bullet.xml" WithDict:nil];
  actionWithDuration: (ccTime)duration scale:(float) s
  CCAction *ac1 = [actionWithDuration:1.0f scale:1.2]; 
  CCAction *ac2 = [actionWithDuration:1.0f scale:1.4]; 
  CCAction *ac3 = [actionWithDuration:1.0f scale:1.5]; 
  CCAction *ac4 = [actionWithDuration:1.0f scale:1.4]; 
  CCAction *ac5 = [actionWithDuration:1.0f scale:1.2];
 
  CCCallFunc* ca = [CCCallFunc actionWithTarget:ai_ selector:@selector(onMoveEnd)];
  CCAction *ac = [CCSequence actions:ca1,ca2,ca3,ca4,ca5,nil];
  [bullet addAction:ac Named:"blow"];
  FAI *ai = [[PlayerAI alloc] initWithFObject:bullet];
  [bullet setAi:ai];
  [bullet startAi];
  [bullet setPosition:[self position]]; 

  CCSprite *sp = [bullet getSpriteByName:@"bullet"];
  [sp setScale:0.5];
  [sp setRotation:90-angle];
  
  [bullet moveBy:dp Dur:0.2];
}

-(void)onTimer{

}
@end


@implementation BulletAI
-(id)initWithFObject:(FObject*)obj{
  [super init];
  object_ = obj;
  [[CCScheduler sharedScheduler] scheduleSelector:@selector(onTimer) forTarget:self interval:0.1f paused:false];
  return self;
}

-(void)onMoveEnd{
  [object_ hideSprite:@"bullet"];
  [object_ runAction:@"blow" WithSprite:@"net"];
}

-(void)onActionEnd:(id)sender Data:(NSString*)data{
  [[CCScheduler sharedScheduler] unscheduleSelector:@selector(onTimer) forTarget:self]; 
  [[object_ parent] removeChild:o cleanup:true];
}

-(void)onTimer{
  CGPoint bp = [object_ position];
  NSMutableArray *fishes = [[FishManager sharedFishManager] fishes];
  for(FishData *fd in fishes){
    CGRect rect = [fd getObjectRect];
    if(CGRectContainsPoint(rect, bp)){
      [object_ runAction:@"blow" WithSprite:@"net"];
      break;
    }
  }
}
@end


@implementation PlayerAI
-(id)initWithFObject:(FObject*)obj{
  [super init];
  object_ = obj;
  [[CCScheduler sharedScheduler] scheduleSelector:@selector(onTimer) forTarget:self interval:0.1f paused:false];
  return self;
}

-(void)onMoveEnd{
  CCSprite *sp = [object_ getSpriteByName:@"bullet"];
  [sp setVisible:false];

  CGPoint p = [sp position];
  sp = [object_ getSpriteByName:@"net"];
  [sp setPosition:p];
  [sp setScale:0.5];

  [object_ runAction:@"blow" WithSprite:@"net"];
}

-(void)onActionEnd:(id)sender Data:(NSString*)data{
  //[(Player*)object_ setFiring:false];
  CCSprite *sp = [object_ getSpriteByName:@"bullet"];
  [sp setPosition:ccp(0,0)];
  sp = [object_ getSpriteByName:@"net"];
  [sp setVisible:false];
}

-(void)onTimer{
  Player* pl = (Player*)object_;
  if(![pl firing]){
    return;
  }
  CGPoint bp = [pl getWorldPointWithName:@"bullet"];
  NSMutableArray *fishes = [[FishManager sharedFishManager] fishes];
  for(FishData *fd in fishes){
    CGRect rect = [fd getObjectRect];
    if(CGRectContainsPoint(rect, bp)){
      [pl runAction:@"blow" WithSprite:@"bullet"];
      break;
    }
  }
}
@end
